<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE topic
  PUBLIC "-//OASIS//DTD DITA Composite//EN" "ditabase.dtd">
<topic id="topic1">
  <title id="dz20941">REINDEX</title>
  <body>
    <p id="sql_command_desc">Rebuilds indexes.</p>
    <section id="section2"
      ><title>Synopsis</title><codeblock id="sql_command_synopsis">REINDEX {INDEX | TABLE | DATABASE | SYSTEM} <varname>name</varname></codeblock></section>
    <section id="section3"><title>Description</title><p><codeph>REINDEX</codeph> rebuilds an index
        using the data stored in the index's table, replacing the old copy of the index. There are
        several scenarios in which to use <codeph>REINDEX</codeph>: </p><ul>
        <li id="dz154728">An index has become bloated, that is, it contains many empty or
          nearly-empty pages. This can occur with B-tree indexes in Greenplum Database under certain
          uncommon access patterns. <codeph>REINDEX</codeph> provides a way to reduce the space
          consumption of the index by writing a new version of the index without the dead
          pages.</li>
        <li id="dz154739">You have altered the <codeph>FILLFACTOR</codeph> storage parameter for an
          index, and wish to ensure that the change has taken full effect. </li>
      </ul></section>
    <section id="section4"><title>Parameters</title><parml>
        <plentry>
          <pt>INDEX</pt>
          <pd>Recreate the specified index.</pd>
        </plentry>
        <plentry>
          <pt>TABLE</pt>
          <pd>Recreate all indexes of the specified table. If the table has a secondary TOAST table,
            that is reindexed as well.</pd>
        </plentry>
        <plentry>
          <pt>DATABASE</pt>
          <pd>Recreate all indexes within the current database. Indexes on shared system catalogs
            are also processed. This form of <codeph>REINDEX</codeph> cannot be run inside a
            transaction block.</pd>
        </plentry>
        <plentry>
          <pt>SYSTEM</pt>
          <pd>Recreate all indexes on system catalogs within the current database. Indexes on shared
            system catalogs are included. Indexes on user tables are not processed. This form of
              <codeph>REINDEX</codeph> cannot be run inside a transaction block.</pd>
        </plentry>
        <plentry>
          <pt><varname>name</varname></pt>
          <pd>The name of the specific index, table, or database to be reindexed. Index and table
            names may be schema-qualified. Presently, <codeph>REINDEX DATABASE</codeph> and
              <codeph>REINDEX SYSTEM</codeph> can only reindex the current database, so their
            parameter must match the current database's name.</pd>
        </plentry>
      </parml></section>
    <section id="section5">
      <title>Notes</title>
      <p><codeph>REINDEX</codeph> causes locking of system catalog tables, which could
        affect currently running queries. To avoid disrupting ongoing business operations, schedule
        the <codeph>REINDEX</codeph> operation during a period of low activity.</p>
      <p><codeph>REINDEX</codeph> is similar to a drop and recreate of the index in that the index
        contents are rebuilt from scratch. However, the locking considerations are rather different.
          <codeph>REINDEX</codeph> locks out writes but not reads of the index's parent table. It
        also takes an exclusive lock on the specific index being processed, which will block reads
        that attempt to use that index. In contrast, <codeph>DROP INDEX</codeph> momentarily takes
        an exclusive lock on the parent table, blocking both writes and reads. The subsequent
          <codeph>CREATE INDEX</codeph> locks out writes but not reads; since the index is not
        there, no read will attempt to use it, meaning that there will be no blocking but reads may
        be forced into expensive sequential scans. </p>
      <p>Reindexing a single index or table requires being the owner of that index or table.
        Reindexing a database requires being the owner of the database (note that the owner can
        therefore rebuild indexes of tables owned by other users). Of course, superusers can always
        reindex anything.</p>
      <p><codeph>REINDEX</codeph> does not update the <codeph>reltuples</codeph> and
          <codeph>relpages</codeph> statistics for the index. To update those statistics, run
          <codeph>ANALYZE</codeph> on the table after reindexing. </p>
      <p>If you suspect that shared global system catalog indexes are corrupted, they can only be
        reindexed in Greenplum utility mode. The typical symptom of a corrupt shared index is "index
        is not a btree" errors, or else the server crashes immediately at startup due to reliance on
        the corrupted indexes. Contact Greenplum Customer Support for assistance in this
        situation.</p></section>
    <section id="section6"><title>Examples</title><p>Rebuild a single index:
        </p><codeblock>REINDEX INDEX my_index;</codeblock><p>Rebuild all the indexes on the table
          <codeph>my_table</codeph>: </p><codeblock>REINDEX TABLE my_table;</codeblock></section>
    <section id="section7"><title>Compatibility</title><p>There is no <codeph>REINDEX</codeph>
        command in the SQL standard.</p></section>
    <section id="section8"><title>See Also</title><p><codeph><xref href="CREATE_INDEX.xml#topic1"
            type="topic" format="dita"/></codeph>, <codeph><xref href="DROP_INDEX.xml#topic1"
            type="topic" format="dita"/></codeph>, <codeph><xref href="./VACUUM.xml#topic1"
            type="topic" format="dita"/></codeph></p></section>
  </body>
</topic>
